//
//  MinePersonInfoCell.m
//  WeCommunity
//
//  Created by mac on 2017/6/15.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "MinePersonInfoCell.h"
#import "ContactModel.h"

@interface MinePersonInfoCell ()

@property (nonatomic,strong) UIImageView * headImage;
@property (nonatomic,strong) UIImageView * sexView;
@property (nonatomic,strong) UIImageView * arrowImage;

@property (nonatomic,strong) UIView * vipView;
@property (nonatomic,strong) UIImageView * vipImageView;
@property (nonatomic,strong) UILabel * vipLabel;

@property (nonatomic,strong) UILabel * nickNameLabel;
@property (nonatomic,strong) UILabel * WXUserNameLabel;


@property (nonatomic,strong) UIView * topLineView;
@property (nonatomic,strong) UIView * botLineView;

@property (nonatomic,strong) ContactModel * contact;

@end


@implementation MinePersonInfoCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        CGFloat height = 88;
        
        //头像
        self.headImage = [[UIImageView alloc] initWithFrame:CGRectMake(12, height / 2 - 33, 66, 66)];
        self.headImage.contentMode = UIViewContentModeScaleAspectFill;
        self.headImage.userInteractionEnabled = YES;
        self.headImage.layer.cornerRadius = 6.0;
        self.headImage.clipsToBounds = YES;
        self.headImage.exclusiveTouch = YES;
        [self addSubview:self.headImage];
        //点击手势识别器
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processGesture:)];
        [self.headImage addGestureRecognizer:tap];
        
        
        
        //名字
        self.nickNameLabel = [self getCustomLabel:PFR_FONT(16)];
        self.nickNameLabel.frame = CGRectMake(CGRectGetMaxX(self.headImage.frame) + 12, 22, KScreen_Width - CGRectGetMaxX(self.headImage.frame) - 40, 22);
        [self addSubview:self.nickNameLabel];
        
        //vip等级
        self.vipView = [[UIView alloc] init];
        self.vipView.backgroundColor = [UIColor colorWithHex:0xfff67f alpha:1.0];
        self.vipView.layer.cornerRadius = 4.0;
        self.vipView.clipsToBounds = YES;
        [self addSubview:self.vipView];
        
        self.vipImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 22)];
        [self.vipView addSubview:self.vipImageView];
        
        self.vipLabel = [self getCustomLabel:PFR_FONT(12)];
        self.vipLabel.frame = CGRectMake(24, 0,56, 22);
        self.vipLabel.textAlignment = NSTextAlignmentCenter;
        [self.vipView addSubview:self.vipLabel];
        
        //性别
        self.sexView = [[UIImageView alloc] init];
        self.sexView.contentMode = UIViewContentModeScaleAspectFill;
        self.sexView.clipsToBounds = YES;
        [self addSubview:self.sexView];
        
        //微信号
        self.WXUserNameLabel = [self getCustomLabel:PFR_FONT(14)];
        self.WXUserNameLabel.frame = CGRectMake(CGRectGetMaxX(self.headImage.frame) + 12 , CGRectGetMaxY(self.nickNameLabel.frame) + 8, KScreen_Width - CGRectGetMaxX(self.headImage.frame) - 30, 22);
        [self addSubview:self.WXUserNameLabel];
        
        
        self.arrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(KScreen_Width - 22, height / 2 - 8, 10, 17)];
        self.arrowImage.contentMode = UIViewContentModeScaleAspectFill;
        self.arrowImage.clipsToBounds = YES;
        self.arrowImage.image = [UIImage imageNamed:@"arrow_right"];
        [self addSubview:self.arrowImage];
        
        
        self.topLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreen_Width, 0.5)];
        self.topLineView .backgroundColor = LINE_COLOR;
        [self.contentView addSubview:self.topLineView ];
        
        self.botLineView = [[UIView alloc] initWithFrame:CGRectMake(0, height - 1 , KScreen_Width, 0.5)];
        self.botLineView .backgroundColor = LINE_COLOR;
        [self.contentView addSubview:self.botLineView ];
        
    }
    return self;
}

-(UILabel *)getCustomLabel:(UIFont *)font
{
    UILabel * label = [[UILabel alloc] init];
    label.font = font;
    label.textColor = [UIColor blackColor];
    return label;
}


-(void)creatCellWithModel:(id )model
{
    self.contact = model;

    self.nickNameLabel.frame = CGRectMake(CGRectGetMaxX(self.headImage.frame) + 12, 22, KScreen_Width - CGRectGetMaxX(self.headImage.frame) - 40, 22);
    
    if (![UserInfoManger shared].isLogin) {
        [self.headImage setImage:[UIImage imageNamed:@"NotloginHead"]];
        self.nickNameLabel.text = KNSLocal(@"NotLogin");
        self.WXUserNameLabel.hidden = YES;
        self.sexView.hidden = YES;
        self.vipView.hidden = YES;
    }else{
        
        
        [self.headImage sd_setImageWithURL:LOADINGTHUMBIMG_FIXED_URL(self.contact.headImage)
                          placeholderImage:[UIImage imageNamed:@"placeholder"]
                                   options:SDWebImageRetryFailed
                                 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                     
                                 }];
        
        self.nickNameLabel.text = self.contact.nickName;
        self.nickNameLabel.numberOfLines = 1;
        [self.nickNameLabel sizeToFit];
        
        self.WXUserNameLabel.text = [NSString stringWithFormat:@"%@%@",KNSLocal(@"WXName:"),self.contact.WXUserName];
        self.WXUserNameLabel.hidden = NO;
    
        CGFloat sexPointX = 0;
        if (self.contact.userVipLevel > 0) {
            self.vipView.hidden = NO;
            self.vipView.frame = CGRectMake(CGRectGetMaxX(self.nickNameLabel.frame) + 4, 22, 80, 22);
            self.vipImageView.image = self.contact.userVipLevelImage;
            self.vipLabel.text = self.contact.userVipLevelStr;
            sexPointX = CGRectGetMaxX(self.vipView.frame) + 4;
        }else{
            self.vipView.hidden = YES;
            sexPointX = CGRectGetMaxX(self.nickNameLabel.frame) + 4;
        }
        
        self.sexView.hidden = NO;
        self.sexView.frame = CGRectMake(sexPointX, 25, 16, 16);
        if ([self.contact.sex integerValue] == 1){
            self.sexView.image = [UIImage imageNamed:@"sex_men"];
        }else if ([self.contact.sex integerValue] == 2){
            self.sexView.image = [UIImage imageNamed:@"sex_women"];
        }else{
            self.sexView.hidden = YES;
        }
        
    }
}

-(void)needAddTopLine:(BOOL)topLine addBottomLine:(BOOL)bottomLine
{
    self.topLineView.hidden = !topLine;
    self.botLineView.hidden = !bottomLine;
}

-(void)needAddArrow:(BOOL)arrow
{
    self.arrowImage.hidden = !arrow;
}

#pragma mark - 手势
- (void) processGesture:(UISwipeGestureRecognizer *)gesture
{
    //点击手势
    if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
        
        if ([self.delegate respondsToSelector:@selector(processPersonInfoCellWithSender:clickCell:)]) {
            [self.delegate processPersonInfoCellWithSender:gesture.view clickCell:self];
        }
        
    }
}


@end
